var flag1 = 0;
var datass=[];
var indexdata=[];
var deletedata=[];
var stringdata;
var stringdeletedata;
var kk;
var indexdatas;
var tabpanel;
var isopen = false;
var mark;
/*
 * @Description: 预加载
 * @param :
 */
$(function(){
	$('#drag_box').unbind();
	fnResize();
	fnTabButton();
	fnShowImage();
	/*var getIndexNum = sessionStorage.getItem("tabLiNum");
	$(".nav-tabs li").eq(getIndexNum).addClass('active').siblings().removeClass('active');
	console.log($(".tab-pane").eq(getIndexNum));
	$("#Section"+getIndexNum+1).addClass('active').siblings().removeClass('active');*/
	/*setTimeout(function() {//延迟加载
		$("#tab1").removeClass('hd').addClass('sh');
	}, 150);*/
	getDesks();
	tdragDelete();
	getDesk();
	getDisplay();//拖拽1调用
	initTabs();
	getsomeDesk();
	//拖拽2及添加数据start
    var d = $('.drag').dad();
	var target = $('.dropzone');
	d.addDropzone(target, function(e){
		e.find('span').appendTo(target);
		e.remove();
	});
	//拖拽2及添加数据end
	//拖拽删除 start
	/*var jq20 = $('.jq20').dad();
	var targett = $('.deletedrop');
	jq20.addDropzone(targett, function(e){
		e.find('span i').css("fontSize","12px");
		e.find('span a').css("fontSize","12px");
		e.find('span').appendTo(targett);
		e.remove();
	});*/
	//拖拽删除 end
	var getIndexNum = sessionStorage.getItem("tabLiNum");
	$(".nav-tabs li").eq(getIndexNum).addClass('active').siblings().removeClass('active');
	$(".tab-bd").eq(getIndexNum).show().siblings(".tab-bd").hide();
	//$("#drag_box").unbind("click");
	/*$("#section1").click(function(){
	 parent.location.reload();
	}*/
});
/*
 * @Description: 浏览器窗口大小改变后：
 * 				 class为container-fluid，row，divClassLeft，divClassRight的高度随之改变，
 * 				 divClassLeft的最大高度和最小高度随之改变
 * @param :
 */
function fnResize() {
	var height = $(window).height() - 10;//获取浏览器窗口大小
	document.getElementsByClassName("blank_bg")[0].style.height = height +"px";
//	document.getElementsByClassName("row")[0].style.height = height +"px";
	document.getElementsByClassName("divClassRight")[0].style.height = (height - 16) +"px";
}

/*
 * @Description: 点击左边a标签
 * @param :
 */
function fnTabButton() {
	$(".divClassLeft a").click(function() {
		if (flag1 == 0) {
			flag1 == 1;
			$(this).addClass('activeA').siblings().removeClass('activeA');
			var flag = $(this).attr('pk');
			$("#tab" + flag).removeClass('hd').addClass('sh').siblings().removeClass('sh').addClass('hd');
			flag1 = 0;
		} else {
			return false;
		}
	});
	$(".divClassLeft a").click(function() {
		if (flag1 == 0) {
			flag1 == 1;
			$(this).addClass('activeA').siblings().removeClass('activeA');
			var flag = $(this).attr('pk');
			$("#tab" + flag).removeClass('hd').addClass('sh').siblings().removeClass('sh').addClass('hd');
			flag1 = 0;
		} else {
			return false;
		}
	});
}

/*
 * @Description: 图片显示
 * @param :
 */
function fnShowImage() {
	$(".divClassRight img").click(function() {
		$("#showImage").attr({ "src" : $(this).attr('src')});
		$("#alterModal2").modal('show');
	});
}

/*
 * @Description: 浏览器窗口大小自适应
 * @param :
 */
$(window).resize(function() {
	window.setTimeout(function() {
		fnResize();
	}, 0);
});
/*
 * @Description: 工作台添加全部显示
 * @param :
 */
function getDesk() {
	$.ajax({
		url : pathUrl('menu/getCardPool.action'),
		type: "POST",
		dataType : 'json',
		async : false,
		success : function(data) {
			var desk = '';
			//console.log(data);
			$.each(data, function(i, n) {
				//console.log(i);
				//console.log(n.pPid);
				if(n.pUrl=='#'||n.closed==0){
					//console.log("ppp");
					if(n.pId.length==1){
						desk+='<div class="ypc" style="height: 30px; color: #fff;background: #22b7b5;font-size: 12px;margin-bottom: 10px;line-height: 30px;padding-left: 10px;" >'+ n.pName+'</div>'
					}
				}else{
					desk+='<div class="item" ><span class="'+(n.pId)+'" index="'+i+'" >'+n.pName+'</span></div>'
				}
				
			});
			$('.drag').html(desk);
		}
	});
}
/*
 * @Description: 工作台添加部分显示
 * @param :
 */
function getsomeDesk() {
	$.ajax({
		url : pathUrl('menu/desk.action'),
		type: "POST",
		dataType : 'json',
		async : false,
		success : function(data) {
			var desk = '';
			//console.log(data);
			$.each(data, function(i, n) {
					desk+='<span class="'+(n.id)+'">'+n.text+'</span>'
			
			});
			$('.dropzone').append(desk);
		}
	});
}
/*
 * @Description: c
 * @param :
 */
function arrCheck(arr){
	  var newArr = [];
	  for(var i=0;i<arr.length;i++){
		var temp=arr[i];
		var count=0;
		for(var j=0;j<arr.length;j++){
		  if(arr[j]==temp){
			count++;
			arr[j]=-1;
		  }
		}
		if(temp != -1){
		  newArr.push(count)
		}
	  }
	  return newArr;
	}
/*
 * @Description: 点击保存
 * @param :
 */
$("#saves").click(function(){
	console.log( $(".dropzone").find("span"));
	var datas=$(".dropzone").find("span");
	$.each(datas, function(i, n) {
		console.log(datas);
		$(this).attr("index",i+1);
//		console.log($(this).attr("index"));
//		console.log(parseInt(n.className));
		datass.push(parseInt(n.className));
		indexdata.push(parseInt($(this).attr("index")));
	});
	stringdata=ret(datass).join(',');
	indexdatas=ret(indexdata).join(',');
	var check= arrCheck(datass);
	mark=check.includes(2);
	console.log(mark);
	if(mark){
		swal_Tips('重复添加，请重置后再试');
		return false;
	}
	if(datas.length>9){
		swal_Tips('只允许添加9个，请重置后再试');
		return false;
	}
	getTable();
});
$("#reset").click(function(){
	//swal_Tips_Short("操作成功，一秒后刷新");	
	setTimeout(function() {//延迟加载
		location.reload();
	}, 60);
		//location.reload();
});
$("#section1").click(function(){
	console.log("shdkshd");
	location.reload();
});
$("#section4").click(function(){
	console.log("sdjlsdj");
	parent.location.reload();
});
/*
 * @Description: 保存接口
 * @param :
 */
function getTable(){
	$.ajax({
		url:pathUrl("menu/setDesk.action"),//接口
		type:'POST',
		dataType:'JSON',
		async:false,
		data:{ids:stringdata,indexs:indexdatas},//获取其数据
		success : function(data) {
			if(data.code == "0000"){
				swal_Tips_Short(data.message);	
				setTimeout(function() {//延迟加载
					location.reload();
				}, 2000);
			}else{
				swal_Tips(data.message);
			}
		},
		error : function(request) {
			swal_Tips_Error();
		}
	});
}
/*
 * @Description: 查重
 * @param :
 */
var ret= function(arr){
	var res=[arr[0]];
	for(var j=1;j<arr.length;j++){
		var repeat= false;
		for(var i=0;i<res.length;i++){
		   if(arr[j]==res[i]){
			   repeat=true;
			   break;
		   }
		}
		if(!repeat){
		   res.push(arr[j]);
		}
	}
	return res;
};
/*
 * @Description: 工作台删除显示
 * @param :
 */
function getDisplay() {
	$.ajax({
		url : pathUrl('menu/desk.action'),
		type: "POST",
		dataType : 'json',
		async : false,
		success : function(data) {
			var desk = '';
//			console.log(data);
			$.each(data, function(i, n) {
//				console.log(n.id);
				var icon = n.classes;
				/*desk += '<div class="col-md-3 col-sm-3 box" style="margin: 0 30px; float:left;" ><div class="row state-overview" style="float:left;margin:0 20px;"><a addtabs="'+n.id+'" url="'+n.url+'" index="'+(i+1)+'">'
				+'<div class="panel purple" style="width:400px;"><i class="fa '+icon+'" style="font-size:24px;"></i>'
				+'<div class="value" style="font-size:24px;">'+n.text+'</div></div></div></a></div>';*/
					desk+='<div class="item" style="float:left;"><span class="'+n.id+'" ><i class="fa '+icon+'" style="font-size:24px;"></i><a style="color: #fff;font-size: 24px;"  addtabs="'+n.id+'" url="'+pathUrl(n.url)+'">'+n.text+'</a></span></div>';
					
			});
			$('.jq20').html(desk);
		}
	});
}
/*
 * @Description: 工作台删除点击保存
 * @param :
 */
$("#savess").click(function(){
	console.log( $(".deletedrop").find("span"));
	var datas=$(".deletedrop").find("span");
	$.each(datas, function(i, n) {
		console.log(datas);
		console.log(parseInt(n.className));
		deletedata.push(parseInt(n.className));
	});
	stringdeletedata=ret(deletedata).join(',');
	console.log(stringdeletedata);
	deleteTable();
});
/*
 * @Description: 工作台删除保存接口
 * @param :
 */
function deleteTable(){
	$.ajax({
		url:pathUrl("menu/deleteDesk.action"),//接口
		type:'POST',
		dataType:'JSON',
		async:false,
		data:{ids:stringdeletedata},//获取其数据
		success : function(data) {
			if(data.code == "0000"){
				swal_Tips_Short(data.message);	
				setTimeout(function() {//延迟加载
					location.reload();
				}, 2000);
			}else{
				swal_Tips(data.message);
			}
		},
		error : function(request) {
			swal_Tips_Error();
		}
	});
}
/*
 * @Description: tdrag插件工作台显示
 * @param :
 */
function getDesks() {
	$.ajax({
		url : pathUrl('menu/desk.action'),
		type: "POST",
		dataType : 'json',
		async : false,
		success : function(data) {
			var desk = '';
			$.each(data, function(i, n) {
				var icon = n.classes;
				desk += '<div class="box" style="margin: 0 40px; float:left" ><div class="row state-overview" style="float:left;margin:0 20px;"><a style="text-decoration:none;" addtabs="'+n.id+'" url="'+n.url+'" index="'+(i+1)+'">'
				+'<div class="panel purple" style="width:400px;"><i class="fa '+icon+'" style="font-size:24px;"></i>'
				+'<div class="value" style="font-size:24px;text-align: center;">'+n.text+'</div></div></div></a></div>';
			});
			$('#sum_box').html(desk);
			/*setTimeout(function (){
				box();
			},60);*/
		}
	});
}
//tdrag路径点击函数
function initTabs(){
    $("#sum_box a").click(function() {/*
    	if(isopen){*/
    		//isopen=false;
			var id = $(this).attr('url');
			parent.openurl(id,$(this).text(),'<iframe src="'+id+'" width="100%" height="100%" frameborder="0"></iframe>');
			
     /*}*/
	});
    $("#drag_box b").click(function() {
    	/*$('.deletebox').css("z-index":"1!important");*//*
    	if(isopen){
    	isopen=false;*/
    	//$(this).parent().remove();
    	var ypc=$(this).parents('a')[0];
    	console.log($(this).parents('.deletebox')[0])
    	var yy=$(this).parents('.deletebox')[0];
    	//$(yy).css('z-index':'1!improtant;');
    	$(yy).css({ "cssText": "display:none;" });
    	var deleteaddtabs=$(ypc).attr("addtabs");
			$.ajax({
				url:pathUrl("menu/deleteDesk.action"),//接口
				type:'POST',
				dataType:'JSON',
				async:false,
				data:{ids:deleteaddtabs},//获取其数据
				success : function(data) {
					if(data.code == "0000"){
						swal_Tips_Short(data.message);	
						setTimeout(function() {//延迟加载
							location.reload();
						}, 2000);
					}else{
						swal_Tips(data.message);
					}
				},
				error : function(request) {
					swal_Tips_Error();
				}
			});
	});
   
}
/*
 * @Description: tdrag删除插件工作台显示
 * @param :
 */
function tdragDelete() {
	$.ajax({
		url : pathUrl('menu/desk.action'),
		type: "POST",
		dataType : 'json',
		async : false,
		success : function(data) {
			var desk = '';
			$.each(data, function(i, n) {
				var icon = n.classes;
				desk += '<div class="deletebox" style="margin: 0 40px; float:left" ><div class="row state-overview" style="float:left;margin:0 20px;"><a style="text-decoration:none;" addtabs="'+n.id+'" url="'+n.url+'" index="'+(i+1)+'">'
				+'<div class="panel purple" style="width:400px;"><b class="cha"></b><i class="fa '+icon+'" style="font-size:24px;"></i>'
				+'<div class="value" style="font-size:24px;text-align: center;">'+n.text+'</div></div></div></a></div>';
			});
			$('#drag_box').html(desk);
			setTimeout(function (){
				box();
			},60);
		}
	});
}
/*
 * @Description: tdrag插件工作台显示切换
 * @param :
 */
function box(){
	$(".deletebox").Tdrag({
		scope:"#drag_box",
		axis:"all",//上下或者左右
	  	pos:true,
	  	//disable:true,
	    dragChange:true,
	    cbStart:function(Obj,obj){
	    	sl = obj.offsetLeft;
	    	st = obj.offsetTop;
	    },
	    cbEnd:function(Obj,obj){
	    	el = obj.offsetLeft;
	    	et = obj.offsetTop;/*
	    	//isopen = true;*/
	    	if(sl == el && st == et){
	    		isopen = true;
	    	}
	    	setTimeout(function (){
	    		location.reload();
			},1000);
	    	var oNear=Obj.findNearest(obj);
	    	var data;
	        if (oNear) {   //两两交换
	    		var si = $(obj).find("a").attr("index");
	    		var sm = $(obj).find("a").attr("addtabs");
	    		var ni = $(oNear).find("a").attr("index");
	    		var nm = $(oNear).find("a").attr("addtabs");
    			$(obj).find("a").attr("index",ni);
    			$(oNear).find("a").attr("index",si);
    			si = $(obj).find("a").attr("index");
    			ni = $(oNear).find("a").attr("index");
    			$.ajax({
    				url:pathUrl("menu/updateDesk.action"),//接口
    				type:'POST',
    				dataType:'JSON',
    				data:{'si':si,'sm':sm,'ni':ni,'nm':nm},//获取其数据
    				success : function(data) {
    				},
    				error : function(request) {
    				}
    			});
	    	}
	    }//移动结束时候的回调函数
    });
}
$(".nav-tabs li").on('click',function(){
			$(this).addClass('active').siblings().removeClass('active');
			$(".tab-bd").eq($(this).index()).show().siblings(".tab-bd").hide();
			var indexNum = $(this).index(); //所点击li的索引值
			console.log("当前li的下标为：",indexNum); //打印索引值
			 sessionStorage.setItem("tabLiNum",indexNum);  //将(下标名称，索引值)存入session中
})